package com.lordtan.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.lordtan.service.ILogInService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 登陆Action
 * 如果加入struts-rest-plugin这个jar包，则会默认访问index方法
 * @author lordtan
 * @date 2013-1-2
 */
@SuppressWarnings("serial")
public class LogInAction extends ActionSupport {
	private String name; //姓名
	private String password; //密码
	private String email; //email
	private String company; //公司
	private String regCode; //注册码
	
    @Autowired
	private ILogInService logInService;
	private List<Map<String,Object>> result;
	
	{
	    result = new ArrayList<Map<String,Object>>();
    	Map<String,Object> mp = new HashMap<String, Object>();
		mp.put("name", "lordtan");
		mp.put("email", "tanluole@gmail.com");
		result.add(mp);
	}
	
    
    /**
     * 用户登陆，如果已经注册，则跳转至产品添加页面
     * 如果还没有注册，则跳转至注册页面
     * @return
     */
	@Action(value="login",results = {
   	     @Result(name="success",location="/page/bussiness/publishMain.jsp"),
   	     @Result(name="failed",location="/page/Register.jsp")
    })
    public String login(){
    	System.out.println("用户登陆。。。。   name: "+name+"    password:"+password);
    	boolean rel = logInService.checkLogin(name, password);
    	if(rel){
    		System.out.println("User login success 。。。。");
    	}else{
    		System.out.println("User login failed 。。。。");
    		return "failed";
    	}
    	
    	return SUCCESS;
    }
    
	/**
	 * 用户注册页面，后台发布系统与主机ip，主机mac地址和用户公司绑定
	 * 用户注册成功后跳转至登陆界面
	 * @return
	 */
	@Action(value="register",results = {
	   	     @Result(name="success",location="Login.jsp"),
	   	     @Result(name="failed",type="json")
	 })
    public String register(){
//    	boolean isRight = SecretTool.isRightRegCode(email, company, regCode);
//    	if(isRight){
//    		ServerCache.set("isRegister", true); //已注册
//    		return SUCCESS; //注册成功，跳转到登陆页面
//    	}
    	return "failed";
    }
    
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<Map<String, Object>> getResult() {
		return result;
	}

	public void setResult(List<Map<String, Object>> result) {
		this.result = result;
	}
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getRegCode() {
		return regCode;
	}

	public void setRegCode(String regCode) {
		this.regCode = regCode;
	}

    
}
